﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace HslCommunication.BasicFramework
{
	/// <summary>
	/// 字符串加密解密相关的自定义类
	/// </summary>
	// Token: 0x0200019D RID: 413
	public static class SoftSecurity
	{
		/// <summary>
		/// 加密数据，采用对称加密的方式
		/// </summary>
		/// <param name="pToEncrypt">待加密的数据</param>
		/// <returns>加密后的数据</returns>
		// Token: 0x06002104 RID: 8452 RVA: 0x000A089C File Offset: 0x0009EA9C
		internal static string MD5Encrypt(string pToEncrypt)
		{
			return SoftSecurity.MD5Encrypt(pToEncrypt, "zxcvBNMM");
		}

		/// <summary>
		/// 加密数据，采用对称加密的方式
		/// </summary>
		/// <param name="pToEncrypt">待加密的数据</param>
		/// <param name="Password">密钥，长度为8，英文或数字</param>
		/// <returns>加密后的数据</returns>
		// Token: 0x06002105 RID: 8453 RVA: 0x000A08BC File Offset: 0x0009EABC
		public static string MD5Encrypt(string pToEncrypt, string Password)
		{
			DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
			byte[] bytes = Encoding.Default.GetBytes(pToEncrypt);
			descryptoServiceProvider.Key = Encoding.ASCII.GetBytes(Password);
			descryptoServiceProvider.IV = Encoding.ASCII.GetBytes(Password);
			MemoryStream memoryStream = new MemoryStream();
			CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write);
			cryptoStream.Write(bytes, 0, bytes.Length);
			cryptoStream.FlushFinalBlock();
			StringBuilder stringBuilder = new StringBuilder();
			foreach (byte b in memoryStream.ToArray())
			{
				stringBuilder.AppendFormat("{0:X2}", b);
			}
			stringBuilder.ToString();
			return stringBuilder.ToString();
		}

		/// <summary>
		/// 解密过程，使用的是对称的加密
		/// </summary>
		/// <param name="pToDecrypt">等待解密的字符</param>
		/// <returns>返回原密码，如果解密失败，返回‘解密失败’</returns>
		// Token: 0x06002106 RID: 8454 RVA: 0x000A0984 File Offset: 0x0009EB84
		internal static string MD5Decrypt(string pToDecrypt)
		{
			return SoftSecurity.MD5Decrypt(pToDecrypt, "zxcvBNMM");
		}

		/// <summary>
		/// 解密过程，使用的是对称的加密
		/// </summary>
		/// <param name="pToDecrypt">等待解密的字符</param>
		/// <param name="password">密钥，长度为8，英文或数字</param>
		/// <returns>返回原密码，如果解密失败，返回‘解密失败’</returns>
		// Token: 0x06002107 RID: 8455 RVA: 0x000A09A4 File Offset: 0x0009EBA4
		public static string MD5Decrypt(string pToDecrypt, string password)
		{
			bool flag = pToDecrypt == "";
			string result;
			if (flag)
			{
				result = pToDecrypt;
			}
			else
			{
				DESCryptoServiceProvider descryptoServiceProvider = new DESCryptoServiceProvider();
				byte[] array = new byte[pToDecrypt.Length / 2];
				for (int i = 0; i < pToDecrypt.Length / 2; i++)
				{
					int num = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 16);
					array[i] = (byte)num;
				}
				descryptoServiceProvider.Key = Encoding.ASCII.GetBytes(password);
				descryptoServiceProvider.IV = Encoding.ASCII.GetBytes(password);
				MemoryStream memoryStream = new MemoryStream();
				CryptoStream cryptoStream = new CryptoStream(memoryStream, descryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Write);
				cryptoStream.Write(array, 0, array.Length);
				cryptoStream.FlushFinalBlock();
				cryptoStream.Dispose();
				result = Encoding.Default.GetString(memoryStream.ToArray());
			}
			return result;
		}
	}
}
